<#import "../common/layout.ftl" as layout/>
<#import "../common/dataTables.ftl" as dataTables/>
<#import "../common/modern-forms.ftl" as mform/>
<#import "../common/modal.ftl" as modal/>
<!DOCTYPE html>
<html>
<@layout.head title="资产类别管理">
    <@dataTables.head/>
    <@dataTables.buttons_head/>
    <@dataTables.select_head/>
    <@dataTables.fixedHeader_head/>
    <@mform.head/>
    <@select.head/>
</@layout.head>
<body>
<@layout.page>
<div class="main-box">
    <div class="content">
        <div id="searchGroup" class="btn-group" style="position: absolute; ">
            <div style='float:left;margin:5px' class="organizationBox">
                <select class='form-control' id='eqRootClass'>
                    <option value="0">全部一级类别</option>
                    <#list eqRootClass as item>
                        <option value="${item.id?c}">${item.name}-${item.code}</option>
                    </#list>
                </select>
            </div>
            <div style='float:left;margin:5px'>
                <button id="btnAddTopMenu" type="button" class="btn btn-blue btn-sm"
                        style="margin-left: 5px;"
                        onclick="javascript:refreshData();">
                    <i class="fa fa-search"></i> 查询
                </button>
            </div>
        </div>
        <!-- 表格 -->
        <div style="position: relative; top:40px; ">
            <@dataTables.commonTable id="mainTable"/>
        </div>
        <!-- 按钮 -->
        <div id="btnGroup" class="btn-group" style="position: absolute; top:40px;">
            <button id="btnAddTopMenu" type="button" class="btn btn-blue btn-sm" style="margin-left: 5px;"
                    onclick="javascript:newdetail();">
                <i class="fa fa-plus"></i> 添加
            </button>

            <button id="btnAddTopMenu" type="button" class="btn btn-blue btn-sm" style="margin-left: 5px;"
                    onclick="javascript:editdetail();">
                <i class="fa fa-edit"></i> 修改
            </button>
            <button id="btnAddTopMenu" type="button" class="btn btn-blue btn-sm" style="margin-left: 5px;"
                    onclick="javascript:deldetail();">
                <i class="fa fa-minus"></i> 删除
            </button>
        </div>
    </div>
</div>
<!-- 弹出窗 -->
    <@modal.editModal id="modal-detail" buttonId="btndetailSave" width="400px" >
        <@mform.container id="editForm">
        <input id="id" name="id" type="hidden"/>
        <input id="code" name="code" type="hidden"/>
        <input id="superId" name="superId" type="hidden"/>
            <@mform.row>
                <@mform.input_text id='name' name='name' label='类别名称' placeholder='类别名称' icon='fa-edit' />
                <@mform.select2 id='type' name='type' label="类型" >
                    <#list eqType as item>
                        <option value="${item.code}">${item.name}</option>
                    </#list>
                </@mform.select2>
            </@mform.row>
        </@mform.container>
    </@modal.editModal>


<!-- 弹出窗 -->
    <@modal.editModal id="modal-detail2" buttonId="btndetailSave2" width="400px" >
        <@mform.container id="editForm2">
        <input id="id" name="id" type="hidden"/>
        <input id="superId" name="superId" type="hidden"/>
            <@mform.row>
                <@mform.input_text id='name' name='name' label='类别名称' placeholder='类别名称' icon='fa-edit' />
            </@mform.row>
            <@mform.select2 id='type2' name='type' label="类型">
                <#list eqType as item>
                <option value="${item.code}">${item.name}</option>
                </#list>
            </@mform.select2>
            <@mform.row>
                <@mform.input_text id='code' name='code' label='类别编号' placeholder='请输入三位数编码' icon='fa-edit' />
            </@mform.row>
        </@mform.container>
    </@modal.editModal>
</@layout.page>
<@layout.foot>
    <@dataTables.foot/>
    <@dataTables.buttons_foot/>
    <@dataTables.select_foot/>
    <@dataTables.fixedHeader_foot/>
    <@mform.foot/>
<script src="${request.contextPath}/assets/js/jquery.serializejson.min.js?v=3653060703"></script>
<script type="text/javascript">
    var State = {
        <#list state as item>
            "${item.code}": "${item.name}",
        </#list>}
    var eqType = {
        <#list eqType as item>
            "${item.code}": "${item.name}",
        </#list>}
    var columns = [
        {'sWidth': '200px', 'title': 'ID', 'data': 'id'},
        {'sWidth': '200px', 'title': '类别名称', 'data': 'name'},
        {'sWidth': '100px', 'title': '类别编号', 'data': 'code'},
        {'sWidth': '100px', 'title': '类型', 'data': 'type'},
        {'sWidth': '100px', 'title': '创建时间', 'data': 'createTime'},
        {'sWidth': '100px', 'title': '修改时间', 'data': 'updateTime'}
    ]
    var mainTable = $('#mainTable').winningTable({
        columns: columns,
        usePager: false,
        "paging": false,
        pageLength: 10,
        fixedHeader: false,
        searching: true,
        ordering: true,
        "columnDefs": [{
            "targets": [0],
            "visible": false
        }],
        buttons: [

        ],
        "rowCallback": function (row, data, index) {
            $('td', row).eq(2).html((eqType[data.type] || "无"));
        }
    });

    // 刷新
    function refreshData() {
        mainTable.queryData("${request.contextPath}/base/eqClass/queryList", {
            datas: JSON.stringify({
                superId: $("#eqRootClass").val()
            })
        });
    }

    refreshData();

    // 新增
    function newdetail() {
        var superId = $("#eqRootClass").val();
        var superName = $("#eqRootClass").find("option:selected").text();
        if (superId == 0) {
            superName = "新增一级类别"
            $('#modal-detail2').find(".modal-title").text(superName);
            clearForm($('#modal-detail2'));
            $("#modal-detail2 #superId").val(0);
            $("#btndetailSave2").unbind();
            $("#btndetailSave2").click(function () {
                savedetail("modal-detail2");
            });
            $('#modal-detail2').modal();
        } else {
            superName = "新增【" + superName + "】的子类别"
            $('#modal-detail').find(".modal-title").text(superName);
            clearForm($('#modal-detail'));
            $("#modal-detail #superId").val(superId);
            $("#modal-detail #code").val($("#eqRootClass").find("option:selected").text().split("-")[1]);
            $("#btndetailSave").unbind();
            $("#btndetailSave").click(function () {
                savedetail("modal-detail");
            });
            $('#modal-detail').modal();
        }
    }

    // 修改
    function editdetail() {
        var datas = mainTable.getSelectedDatas();
        if (datas == null || datas.length == 0 || datas.length > 1) {
            swal("错误", "请选择一行数据", "error");
            return;
        }
        var $modal = $("#modal-detail");
        $modal.find(".modal-title").text("修改信息");
        clearForm($modal);
        var data = datas[0];
        initFormItemData($("#editForm"), data);
        $modal.modal();
        $("#btndetailSave").unbind();
        $("#btndetailSave").click(function () {
            savedetail("modal-detail");
        });
    }

    // 保存
    function savedetail(modelId) {
        swal({
            title: "确定保存",
            text: "是否确定保存此数据",
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#DD6B55",
            confirmButtonText: "是的,确定",
            cancelButtonText: "取消"
        }).then(function (result) {
            if (!result.value)
                return;
            var requestData = $("#" + modelId + " form").serializeJSON();
            if(requestData.code<100){
                swal("错误", "编码请输入大于99的数字", "error");
                return;
            }
            var params = {datas: JSON.stringify(requestData)};
            $.ajax({
                type: 'POST',
                data: params,
                url: '${request.contextPath}/base/eqClass/save',
                success: function (data) {
                    //成功之后
                    if (data.success) {
                        swal({
                            title: "成功",
                            text: "操作成功!",
                            type: "success",
                            showCancelButton: false,
                            confirmButtonText: "确定"
                        }).then(function (result) {
                            $("#" + modelId).modal('hide');
                            if (modelId == "modal-detail2") {
                                location.reload();
                            }
                            refreshData();
                        });
                    } else {
                        swal("错误", data.errorMsg, "error");
                    }
                },
                error: function (xhr, type) {
                    swal("错误", "系统发生内部错误!请稍后再试!", "error");
                }
            });
        });
    }

    // 删除
    function deldetail() {
        var datas = mainTable.getSelectedDatas();
        if (datas == null || datas.length == 0 || datas.length > 1) {
            swal("错误", "请选择一行数据", "error");
            return;
        }
        var data = datas[0];
        swal({
            title: "确定删除",
            text: "是否确定删除此数据",
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#DD6B55",
            confirmButtonText: "是的,确定",
            cancelButtonText: "取消"
        }).then(function (result) {
            if (!result.value)
                return;
            var requestData = {
                id: data.id
            };
            var params = {datas: JSON.stringify(requestData)};
            $.ajax({
                type: 'POST',
                data: params,
                url: '${request.contextPath}/base/eqClass/delete',
                success: function (data) {
                    //成功之后
                    if (data.success) {
                        swal({
                            title: "成功",
                            text: "操作成功!",
                            type: "success",
                            showCancelButton: false,
                            confirmButtonText: "确定"
                        }).then(function (result) {
                            refreshData();
                        });
                    } else {
                        swal("错误", data.errorMsg, "error");
                    }
                },
                error: function (xhr, type) {
                    swal("错误", "系统发生内部错误!请稍后再试!", "error");
                }
            });
        });
    }
</script>
</@layout.foot>
</body>
</html>